package com.thoughtworks.android.androsphere.framework;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
import com.thoughtworks.android.androsphere.R;

public class NotificationService extends Service {
    private NotificationManager mNM;

    public class LocalBinder extends Binder {
        public NotificationService getService() {
            return NotificationService.this;
        }
    }

    @Override
    public void onCreate() {
        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        mNM.cancel(R.string.service_started);

        Toast.makeText(this, R.string.service_stopped, Toast.LENGTH_SHORT).show();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    private final IBinder mBinder = new LocalBinder();

    public void showNotification(CharSequence contentTitle, CharSequence contentText, Class <?> intendedActivity) {
    	int icon = R.drawable.messagesmall;
    	CharSequence tickerText = "BuddyCircle Notification";
    	long when = System.currentTimeMillis();

    	Notification notification = new Notification(icon, tickerText, when);

    	Context context = getApplicationContext();

    	Intent notificationIntent = new Intent(this, intendedActivity);
    	PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    	notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    	mNM.notify(R.string.service_started, notification);    	
    }
}
